<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use  Markdown;
use Captcha;
use Validator;
class AdminController extends Controller
{
	
	public function __construct()
	{		
		
		$this->middleware('log', ['only' => ['fooAction', 'barAction']]);
	
		//$this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
	}
	
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	
	//验证码
	public function getCode()
	{
		 
		//return Captcha::src();;
		echo Captcha::img();
	}
	 
	//生成验证码
	public function getVc()
	{
		dd (Captcha::check('tbyxm'));
	}	

	//用户显示页面
     public function index()
    {
    	$data = DB::table('user')->orderBy('id','desc')->paginate(2);   	
		return view('index.index',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    //用户添加显示页面
    public function create()
    {
        return view('index.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //用户添加操作
    public function store(Request $request)
    {
     	//判断输入信息是否符合要求  第一种门面方式
     	     	$data =  Validator::make($request->all(), [
    			'title' => 'required|max:20|min:5',
    			'content' => 'required|max:1000|min:10',    			 
    			]); 
    	
    	
    			//不符合要要求提示错误信息 终止执行
     			if ($data->fails()){
    			 	return back()->withErrors("请从新填写");

	}
	//第二种方法 trait 自带的
/*    			   $this->validate($request,[
   						 'title' => 'required|max:20|min:5',
    						'content' => 'required|max:1000|min:10',
    					],[
    						'required' => ': attribute 这是必须填写的',
    					]); */
    			   
    			
		//输入信息正常执行插入数据库
    	$data = $request->except('_token');
    	$data = DB::table('user')->insert($data);
    	if($data){
    		return redirect('/Admin');
    	}
    	else{
    		return back();
    	}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //用户显示阅读页面
    public function show($id)
    {
    	 $data = DB::table('user')->where('id','=',$id)->first();
    	
   		 $data->content= Markdown::convertToHtml($data->content);
		
       return view('index.show',['data'=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //用户编辑显示页面
    public function edit($id)
    { 	
    	$data = DB::table('user')->where('id','=',$id)->first();
    	return view('index.edit',['data'=>$data]);
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //用户编辑操作
    public function update(Request $request, $id)
    {

         $data = DB::table('user')->where('id','=',$id)->update(['title'=>$request->title,'content'=>$request->content]);
         return redirect('/Admin');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
   // 用户删除操作
    public function destroy($id)
    {
        $data = DB::table('user')->where('id',$id)->delete();
        return redirect('/Admin');
    }
    


    
    
    
      
    
}
